<?php

defined('SYSPATH') or die('Access error!');

class Controller_Shop_Seller_Goods extends Controller_Shop_User_Goods
{

    public function action_onsale()
    {
        $this->where['is_onsale'] = 1;
        $this->where['is_delete'] = 0;
        $this->display_goods_list();
    }

    public function action_downsale()
    {
        $this->where['is_onsale'] = 0;
        $this->where['is_delete'] = 0;
        $this->display_goods_list();
    }

    public function action_recycle()
    {
        $this->where['is_delete'] = 1;
        $this->display_goods_list();
    }

    //用户添加产品
    public function action_add()
    {
        if ($_POST)
        {
            $_POST['add_time'] = Date::get_time();
            $this->save($_POST);
        }
        Assets::js('tinymce', $this->template->media_path . 'js/kindeditor/kindeditor-min.js');
        //获取缓存文件
        $this->template->assign('category_option', $this->get_category_cache_option('goods'));

        //当前位置
        $this->set_define_position(__('seller manager'));
        Breadcrumbs::add(__('add goods'));
        $this->template->view('shop_goods_add');
    }

    public function action_edit()
    {
        $id = intval($this->request->param('id'));
        $this->check_user_goods($id);
        if ($_POST)
        {
            $this->save($_POST, array('id' => $id));
        }

        $goods_detail = $this->goods->get_goods_detail($id);
        Assets::js('tinymce', $this->template->media_path . 'js/kindeditor/kindeditor-min.js');

        //当前位置
        $this->set_define_position(__('edit goods'));
        //获取缓存文件
        $this->template->assign('category_option', $this->get_category_cache_option('goods', $goods_detail['category_id']));
        $this->template->assign('data', $goods_detail);
        $this->template->view('shop_goods_edit');
    }

    public function handle_post($data)
    {
        //根据用户的id获取店铺的id
        $store_id = Model::factory('shop_store')->get_var(array('user_id' => $this->user_detail['id']), 'id');
        if (!empty($store_id))
        {
            $data['store_id'] = $store_id;
        }
        $data['user_id'] = $this->user_detail['id'];
        $data['account'] = Arr::get($data, 'account', 0);
        $data['name'] = HTML::entities(Arr::get($data, 'name', ''));
        $data['content'] = Arr::get($data, 'content', '');
        $data['content'] = HTML::sanitize_content(htmlspecialchars_decode($data['content']));
        $data['account_type'] = Arr::get($data, 'account_type', 'free');
        $data['update_time'] = Date::get_time();
        $data['category_id'] = $data['category_id'];
//        if (Arr::validate_arr($category_id_arr))
//        {
//            $category_id = $this->handle_parent_id($category_id_arr);
//        }
//        else
//        {
//            $category_id = SHOP_CATEGORY_ID;
//        }
//        $data['category_id'] = $category_id;
        return parent::handle_post($data);
    }

    public function save($post, $where_arr = array())
    {
        $data = $this->handle_post($post);
        if (!$this->goods->validate($data))
        {
            Message::error(__('validate error'));
            $this->request->redirect(Route::get('shop/seller/goods')->uri(array('action' => 'add')));
        }
        if (Arr::validate_arr($where_arr))
        {

            $id = $where_arr['id'];
            if ($id)
            {
                //处理相关的附件和图片
                if (isset($_FILES) && $_FILES['goods_images']['tmp_name'] != '')
                {
                    //删除已经存在的附件包括空间里的文件
                    $this->goods->delete_attachment($id);
                    $time_path = File::create_time_path();
                    $file_detail = $this->save_simple_detail(SHOP_GOODS_IMAGES_SAVE_PATH . $this->user_detail['id'] . '/' . $time_path, 'goods_images');
                    if (Arr::validate_arr($file_detail))
                    {
                        foreach ($file_detail as $value)
                        {
                            $att_arr = array();
                            $att_arr['module'] = 'shop';
                            $att_arr['file_name'] = $value['file_name'];
                            $att_arr['file_size'] = $value['file_size'];
                            $att_arr['file_ext'] = $value['file_ext'];
                            $att_arr['file_path'] = File::change_path($time_path);
                            $att_arr['file_realname'] = $value['file_realname'];
                            $att_arr['user_id'] = $value['user_id'];
                            $att_arr['add_ip'] = $value['ip'];
                            $att_arr['add_time'] = Date::get_time();
                            $att_arr['is_image'] = $value['is_image'];
                            //添加附件
                            $attachment_id = $this->goods->add_attachment($att_arr);
                            if ($attachment_id)
                            {
                                //添加产品和附件的关联关系
                                $this->goods->add_goods_attachment($id, $attachment_id);
                            }
                        }
                    }
                }
            }

            if ($this->goods->update($data, array('id' => $where_arr['id'])))
            {
                Message::success(__('update success'));
                $this->request->redirect(Route::get('shop/seller/goods')->uri(array('action' => 'edit', 'id' => $where_arr['id'])));
            }
        }
        else
        {
            $id = $this->goods->insert($data);
            if ($id)
            {
                //处理相关的附件和图片
                if (isset($_FILES) && $_FILES['goods_images']['tmp_name'] != '')
                {
                    $time_path = File::create_time_path();
                    $file_detail = $this->save_simple_detail(SHOP_GOODS_IMAGES_SAVE_PATH . $this->user_detail['id'] . '/' . $time_path, 'goods_images');
                    if (Arr::validate_arr($file_detail))
                    {
                        foreach ($file_detail as $value)
                        {
                            $att_arr = array();
                            $att_arr['module'] = 'shop';
                            $att_arr['file_name'] = $value['file_name'];
                            $att_arr['file_size'] = $value['file_size'];
                            $att_arr['file_ext'] = $value['file_ext'];
                            $att_arr['file_path'] = File::change_path($time_path);
                            $att_arr['file_realname'] = $value['file_realname'];
                            $att_arr['user_id'] = $value['user_id'];
                            $att_arr['add_ip'] = $value['ip'];
                            $att_arr['add_time'] = Date::get_time();
                            $att_arr['is_image'] = $value['is_image'];
                            //添加附件
                            $attachment_id = $this->goods->add_attachment($att_arr);
                            if ($attachment_id)
                            {
                                //添加产品和附件的关联关系
                                $this->goods->add_goods_attachment($id, $attachment_id);
                            }
                        }
                    }
                }

                //初始化相关的静态数据
                Model::factory('shop_goods_static')->insert(array('goods_id' => $id));
                Message::success(__('goods add success'));
                $this->request->redirect(Route::get('shop/seller/goods')->uri(array('action' => 'add')));
            }
        }
    }

}